package com.redijedi.tapestry.components;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.tapestry.ComponentResources;
import org.apache.tapestry.MarkupWriter;
import org.apache.tapestry.annotations.AfterRenderTemplate;
import org.apache.tapestry.annotations.BeforeRenderTemplate;
import org.apache.tapestry.annotations.Component;
import org.apache.tapestry.annotations.Environmental;
import org.apache.tapestry.annotations.Inject;
import org.apache.tapestry.annotations.OnEvent;
import org.apache.tapestry.annotations.Parameter;
import org.apache.tapestry.annotations.Retain;
import org.apache.tapestry.annotations.SetupRender;
import org.apache.tapestry.corelib.components.ActionLink;
import org.apache.tapestry.corelib.components.Loop;

/**
 * @author torr
 * 
 */
public class TabNavigation {

	@Inject
	private ComponentResources _resources;

	@Environmental
	private Map<String, Object> _data;

	@Parameter(value = "prop:componentResources.id", defaultPrefix = "literal")
	private String _id;

	@Parameter(value = "prop:componentResources.elementName", defaultPrefix = "literal")
	private String _elementName;

	@Parameter(defaultPrefix = "literal")
	private String _panels;

	@Retain
	private String _activatedPanelId;

	@Component(parameters = { "source=links", "value=currentLink" })
	@SuppressWarnings("unused")
	private Loop _linkLoop;

	@Component(parameters = { "title=currentLink.title",
			"context=currentLink.id" })
	@SuppressWarnings("unused")
	private ActionLink _tabSelect;

	private TabPanel _currentLink;

	private List<TabPanel> _tabPanels = new ArrayList<TabPanel>();

	public String getId() {
		return _id == null ? _resources.getNestedId() : _id;
	}

	public List<TabPanel> getLinks() {
		return _tabPanels;
	}

	public TabPanel getCurrentLink() {
		return _currentLink;
	}

	public void setCurrentLink(TabPanel value) {
		_currentLink = value;
	}

	@SetupRender
	void setupComponent() {
		if (_tabPanels.size() > 0) {
			_tabPanels.clear();
		}
		for (String id : getPanelIds()) {
			TabPanel p = (TabPanel) _resources.getPage()
					.getComponentResources().getEmbeddedComponent(id);
			_tabPanels.add(p);
		}
		if (_activatedPanelId != null) {
			_data.remove("activePanelId");
			_data.put("activePanelId", _activatedPanelId);
		}
	}

	@BeforeRenderTemplate
	void renderBeginTag(MarkupWriter writer) {
		if (_elementName != null) {
			writer.element(_elementName);
			writer.attributes("id", getId());
			_resources.renderInformalParameters(writer);
		}
	}

	@AfterRenderTemplate
	void renderEndTag(MarkupWriter writer) {
		if (_elementName != null) {
			writer.end();
		}
	}

	@OnEvent(component = "tabSelect")
	void valueChosen(String panelId) {
		_activatedPanelId = panelId;
	}

	private List<String> getPanelIds() {
		List<String> ids = new ArrayList<String>();
		if (_panels != null && _panels.length() > 0) {
			String[] tokens = _panels.split(",");
			for (String t : tokens) {
				ids.add(t.trim());
			}
		}
		return ids;
	}
}
